package com.aps.apspda.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import com.aps.apspda.R;
import com.aps.apspda.base.BaseActivity;
import com.aps.apspda.callback.DialogEntityCallBack;
import com.aps.apspda.callback.EntityCallBack;
import com.aps.apspda.entity.BaseEntity;
import com.aps.apspda.entity.FinishLotEntity;
import com.aps.apspda.myview.ClearEditText;
import com.aps.apspda.myview.TitleView;
import com.aps.apspda.utils.AntiShake;
import com.aps.apspda.utils.NetUtils;
import com.aps.apspda.utils.StaticMembers;
import com.aps.apspda.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangeCardActivity extends BaseActivity {

    @BindView(R.id.topView)
    TitleView topView;
    @BindView(R.id.etCardNum)
    ClearEditText etCardNum;

    private String blockNum = "";
    private String lotid="";//add by bunny
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_change_card;
    }

    @Override
    protected void initView() {
        topView.setTitle("更换卡环(" + StaticMembers.CUR_USER.getUSERNAME() + ")");
        topView.setTitleMode(TitleView.NORMAL_TEXT_MODE);
        topView.setRightListener("确定更换", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AntiShake.check(v.getId())) {    //判断是否多次点击
                    ToastUtils.showFreeToast("请勿重复点击",
                            ChangeCardActivity.this, false, Toast.LENGTH_SHORT);
                    return;
                }
                if (etCardNum.getText().toString().trim().length() == 0) {
                    ToastUtils.showFreeToast("请填写片号",
                            ChangeCardActivity.this, false, Toast.LENGTH_SHORT);
                    return;
                }
                if (blockNum.length() == 0) {
                    ToastUtils.showFreeToast("请选择Block数量",
                            ChangeCardActivity.this, false, Toast.LENGTH_SHORT);
                    return;
                }
                requestData();
            }
        });
        //add by bunny
        etCardNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() > 12&&s.toString().contains(";")) {
                        String codedata= s.toString();
                        String s_lotid="";
                        s_lotid= codedata.substring(0,codedata.indexOf(";"));
                        if(!s_lotid.equals(lotid)) {
                            ToastUtils.showFreeToast("批次号不符，请确认", ChangeCardActivity.this, false, Toast.LENGTH_SHORT);
                            return ;
                        }
                        etCardNum.setText(codedata.substring(codedata.indexOf(";")+1,codedata.length()));
                        etCardNum.setSelection(etCardNum.getText().length());
                    }
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    protected void initData() {
        //取得启动该Activity的Intent对象 add by bunny
        Intent intent =getIntent();
        lotid =intent.getStringExtra("LotId");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    private void requestData() {
        EntityCallBack<BaseEntity<FinishLotEntity>> callBack = new DialogEntityCallBack<BaseEntity<FinishLotEntity>>
                (new TypeToken<BaseEntity<FinishLotEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<FinishLotEntity>> response) {
                if (response.body().isSuccess(ChangeCardActivity.this)) {
                    loadSuccess(response.body().getMessage());
                    ChangeCardActivity.this.finish();
                } else {
                    loadFail(response.body().getMessage());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<FinishLotEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "ChangeBlockC2E");
            }
        };

        Map<String, String> map = new HashMap<>();
        map.put("EQPID", getEqpID());
        map.put("Block", blockNum);
        map.put("PIECENO", etCardNum.getText().toString().trim());
        NetUtils.requestNet(this, "/ChangeBlockC2E", map, callBack);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick({R.id.rbOne, R.id.rbTwo, R.id.rbFour,R.id.rbFourtoTwo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rbOne:
                blockNum = "1";
                break;
            case R.id.rbTwo:
                blockNum = "2";
                break;
            case R.id.rbFour:
                blockNum = "4";
                break;
            case R.id.rbFourtoTwo:
                blockNum = "4-2";
                break;
        }
    }
}
